﻿using System;
using FlashCards.Dto;
using System.Collections.Generic;
using FlashCards.SL.FlashCardRepeater.Utils.Interfaces;
using FlashCards.SL.FlashCardRepeater.Utils;
using System.Windows.Input;
using FlashCards.SL.FlashCardRepeater.ViewModels.Commands;
using System.Xml.Serialization;
using System.IO;
using System.Windows;

namespace FlashCards.SL.FlashCardRepeater.ViewModels
{
    public class FlashcardSetViewModel : BaseViewModel
    {
        FlashcardSet _set;
        Repeat _repeat;
        IFlashcardsSelector _selector;

        public FlashcardSetViewModel(FlashcardSet set, int userId, Type selectorType)
        {
            _repeat = new Repeat()
            {
                RepeatDate = DateTime.Now,
                Items = new List<RepeatItem>(),
                UserId = userId,
                FlashcardSetId = set.Id
            };

            _set = set;
            InitializeFlashcards(set);
            InitializeCommands();
            _selector = Activator.CreateInstance(selectorType, Flashcards) as IFlashcardsSelector;
            CurrentFlashcard = _selector.GetNext();
            FlashcardsUntilEnd = _selector.GetFlashcardsUntilEnd();
        }

        #region ######## PROPERTIES #############

        public event EventHandler<EndRepeatEventArgs> OnEndRepeatCompleted;
        public List<FlashcardViewModel> Flashcards { get; set; }

        public string Name
        {
            get { return _set.Name; }
        }

        private FlashcardViewModel _currentFlashcard;
        public FlashcardViewModel CurrentFlashcard
        {
            get { return _currentFlashcard; }
            set { _currentFlashcard = value; OnPropertyChanged("CurrentFlashcard"); }
        }

        private int _flashcardsUntilEnd;
        public int FlashcardsUntilEnd
        {
            get { return _flashcardsUntilEnd; }
            set { _flashcardsUntilEnd = value; OnPropertyChanged("FlashcardsUntilEnd"); }
        }

        #endregion

        #region ################### METHODS ########################

        #region ################## Initialization ##################

        void InitializeFlashcards(FlashcardSet set)
        {
            Flashcards = new List<FlashcardViewModel>();

            foreach (var item in set.Flashcards)
            {
                var flashcardModel = new FlashcardViewModel(item);
                flashcardModel.FlashcardClosing += new EventHandler<FlashcardClosingEventArgs>(flashcardModel_FlashcardClosing);
                Flashcards.Add(flashcardModel);
            }
        }

        void flashcardModel_FlashcardClosing(object sender, FlashcardClosingEventArgs e)
        {
            _repeat.Items.Add(new RepeatItem
                {
                    RepeatMark = e.Mark,
                    FlashcardId = e.Flashcard.Id,
                    IsCopied = false
                }
            );
            CurrentFlashcard = _selector.GetNext();
            if (CurrentFlashcard == null)
            {
                //TODO: zakonczenie powtorki
                EndRepeatCommand.Execute(null);
            }
            FlashcardsUntilEnd = _selector.GetFlashcardsUntilEnd();
        }

        #endregion

        #endregion

        #region COMMANDS

        public ICommand EndRepeatCommand { get; private set; }

        void InitializeCommands()
        {
            EndRepeatCommand = new Command(EndRepeat);
        }

        void EndRepeat()
        {

            if (OnEndRepeatCompleted != null)
                OnEndRepeatCompleted.Invoke(this, new EndRepeatEventArgs(_repeat, _set));
        }

        #endregion
    }
}
